package  {
	import flash.display.DisplayObject;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	
	public class DisplayerTwo extends Sprite {
		
		private var _banners:Array;
		private var _stageHeight:Number;
		private var _stageWidth:Number;
		private var _contentTotalHeight:Number = 0;
		private var _contentTotalWidth:Number = 0;
		private var _verticalBannerSpace:Number = 0;
		private var _lastY:Number = 0;
		private var _dcRef;
		private var _evenHSpace:Boolean = false;
		private var _hSpace:int = 5;
		
		public function DisplayerTwo(arrayOfBanners:Array, stageHeight:Number, stageWidth:Number, evenHSpace:Boolean = false, hSpace:int = 5) {
			//get reference to main document
			_dcRef = DocumentClass._base;
			
			//set variables equal to function arguments
			_banners = arrayOfBanners;
			_stageHeight = stageHeight;
			_stageWidth = stageWidth;
			_evenHSpace = evenHSpace;
			_hSpace = hSpace;
			
			//prefill lastY with minus first banner height
			var tempForY:Array = _banners[0];
			_lastY -= tempForY[0].height;
			
			//loop trough banners to calculate total height of banners combined
			for (var i = 0; i < _banners.length; i++) {
				var tempArray:Array = _banners[i];
				_contentTotalHeight += tempArray[i].height;
			}
			
			//loop through banners to get total width
			var widthTemp:Array = _banners[0];
			for (var x = 0; x < widthTemp.length; x++) {
				_contentTotalWidth += widthTemp[x].width;
			}
			
			//calculate horizontal space
			if (_evenHSpace == true) {
				var totalHSpace:Number = _stageWidth - _contentTotalWidth;
				_hSpace= totalHSpace / (widthTemp.length - 1);
			}
			
			//calculate space between banners
			_verticalBannerSpace = (_stageHeight - _contentTotalHeight) / (_banners.length + 1);
			
			//loop through banners
			for (var j = 0; j < _banners.length; j++) {
				//loop through banner pieces
				var tempJ:Array = _banners[j];
				for (var s = 0; s < tempJ.length; s++) {
					//create a sprite to hold banner piece
					var tempSprite:Sprite = new Sprite();
					tempSprite.name = "sprite" + j + "_" + s;
					tempSprite.addChild(tempJ[s]);
					_dcRef.addChild(tempSprite);
					var positionSprite:DisplayObject = _dcRef.getChildByName("sprite" + j + "_" + s);
					positionSprite.y = (Number(_lastY) + Number(tempJ[s].height) + Number(_verticalBannerSpace));
					positionSprite.x = s * (tempJ[s].width + _hSpace);
					//trace("Sprite created. Name: " + tempSprite.name + " Y: " + positionSprite.y + " X: " + positionSprite.x);
				}
				_lastY += (tempJ[0].height + _verticalBannerSpace);
			}
		}
	}	
}